using FBroSharp;
using FBroSharp.Const;
using FBroSharp.Event;
using FBroSharp.Lib;
using System;
using System.Reflection;

namespace BaseTest
{
    /// <summary>
    /// FBro 初始化事件处理类
    /// ⚠️ 必须是独立文件！不要放在 Program.cs 中！
    /// </summary>
    public class InitEvent : FBroInitEvent
    {
        // ⚠️⚠️⚠️ CRITICAL: 方法签名必须精确匹配！
        // ✅ CORRECT: OnBeforeCommandLineProcessing(string, IFBroSharpCommandLine) - 注意 'I' 前缀！
        
        /// <summary>
        /// 即将处理命令行
        /// </summary>
        public override void OnBeforeCommandLineProcessing(string process_type, IFBroSharpCommandLine cmd)
        {
            // 禁用GPU黑名单和缓存
            cmd.DisableGpuBlockList();
            cmd.DisableGpuCache();

            Console.WriteLine($"[InitEvent] {MethodBase.GetCurrentMethod().Name}: {process_type}");
        }

        /// <summary>
        /// 初始化完毕
        /// </summary>
        public override void OnContextInitialized()
        {
            Console.WriteLine($"[InitEvent] {MethodBase.GetCurrentMethod().Name}: 上下文初始化完成");
        }

        /// <summary>
        /// 子进程启动前
        /// </summary>
        public override void OnBeforeChildProcessLaunch(IFBroSharpCommandLine command_line)
        {
            Console.WriteLine($"[InitEvent] {MethodBase.GetCurrentMethod().Name}: 子进程启动");
        }

        /// <summary>
        /// 接收到进程间消息
        /// </summary>
        public override bool OnProcessMessageReceived(IFBroSharpBrowser browser, IFBroSharpFrame frame, 
            FBroSharpProcessId source_process, IFBroSharpProcessMessage message)
        {
            var list = message.GetArgumentList();
            Console.WriteLine($"[InitEvent] 收到消息: {message.GetName()}");
            // 返回true表示消息已处理
            return true;
        }
    }
}

